package net.chenyan.server.controller.admapi;/**
 * @author chenyan
 * @createTime 2022/4/23 16:06
 * @description description
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenyan.commons.core.R;
import net.chenyan.commons.util.StringUtil;
import net.chenyan.server.po.Album;
import net.chenyan.server.po.Singer;
import net.chenyan.server.service.AlbumService;
import net.chenyan.server.service.SingerService;
import net.chenyan.server.service.UserAlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/4/23 16:06
 */

@Slf4j
@Api(tags = {"专辑接口"})
@RestController
@RequestMapping(value = "/admapi/album")
public class AlbumController {
    @Autowired
    private SingerService singerService;
    @Autowired
    private AlbumService albumService;
    @Autowired
    private UserAlbumService userAlbumService;


    @ApiOperation(value = "根据歌手姓名查找专辑")
    @GetMapping (value = "/getBySingerName")
    public R getAlbumBySingerName(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size, String name) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(name)) {
            queryWrapper.like("name", name);
        }
        Singer singer=singerService.getOne(queryWrapper);
        if(singer == null){
            return R.error("歌手不存在，请重新输入歌手名字");
        }
        int singerId=singer.getId();

        return R.data(albumService.findAllBySingerNamePage(singerId, PageRequest.of(page,size)));
    }

    @ApiOperation(value = "根据专辑名称模糊查找专辑")
    @GetMapping (value = "/getByName")
    public R getAlbumByName( @RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size,String name) {
        if (name==null){
        return R.error("请输入专辑名称");
        }
        return R.data(albumService.findAllByNamePage(PageRequest.of(page,size),name));
    }

    @ApiOperation(value = "根据专辑热度展示专辑")
    @GetMapping ("/findByLikedNum")
    public  R findByLikedNum(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(albumService.findAllByLikedNum(PageRequest.of(page,size)));
    }

    @ApiOperation(value = "根据专辑最新发布时间展示专辑")
    @GetMapping ("/findByCreateTime")
    public  R findByCreateTime(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size) {
        return R.data(albumService.findAllByCreateTime(PageRequest.of(page,size)));
    }

    @ApiOperation(value = "根据专辑id显示专辑评论")
    @GetMapping ("/findAlbumCommentById")
    public  R findAlbumCommentById(@RequestParam(defaultValue = "0")int page, @RequestParam(defaultValue = "5")int size,int albumId) {
        return R.data(userAlbumService.findAllAlbumCommentById(PageRequest.of(page,size),albumId));
    }
}
